package com.sx.mvvmlibs.base.impl

import android.app.Activity
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.viewbinding.ViewBinding


/**
 * @Author: YY
 * @Date: 2022/1/12
 * @Describe: TODO
 */
interface IAppBar {

    companion object {

        /**
         * [appBarLayoutId] 通过 [DataBindingUtil] 将 xml 实例化成 binding,
         * 再和 [contentView] 组合形成最终布局
         */

        fun <AppBarV : ViewDataBinding> inflateRootLayout (
            activity: Activity,
            contentView: View,
            appBarLayoutId: Int
        ): Pair<AppBarV, LinearLayout> {
            // 实例化标题栏布局
            val appBarBinding: AppBarV =
                DataBindingUtil.inflate(activity.layoutInflater, appBarLayoutId, null, false)
            val linear = createRootLayout(activity, appBarBinding, contentView)
            return Pair(appBarBinding, linear)
        }

        /**
         * 将 [appBarBinding] 和 [contentView] 通过 LinearLayout 组合起来形成最终布局
         */
        fun createRootLayout (
            activity: Activity,
            appBarBinding: ViewBinding,
            contentView: View
        ): LinearLayout {
            val linear = LinearLayout(activity)
            linear.orientation = LinearLayout.VERTICAL
            linear.addView(appBarBinding.root)
            // 已有的布局撑满剩下的空间
            val param = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1F)
            contentView.layoutParams = param
            linear.addView(contentView)

            return linear
        }

    }

}